<?php
  include("core/config/init.php");
  include("core/includes/database/mysql.php");
  include("core/includes/ffamax_config.php");
  error_reporting(-1);
  ini_set("display_errors", 0);
  $far_1 = array("core/config/connect.inc.php"
  ,"core/config/language_list.php"
  ,"core/config/paths.inc.php"
  ,"core/classes/class.virtual.shippingratecalculator.php"
  ,"core/classes/class.virtual.paymentmodule.php"
  ,"core/classes/class.xmlnodex.php");

  $far_2 = glob("core/functions/*.php"); 
  $far_3 = glob("core/functions/admin/*.php");
  $far = array_merge($far_1,$far_2,$far_3);
  $cfar = count($far);
  for ($n=0; $n<$cfar; $n++) include ($far[$n]);
  db_connect(DB_HOST, DB_USER, DB_PASS) or die(ERROR_DB_INIT);
  db_select_db(DB_NAME) or die(db_error());
  settingDefineConstants();
  $_POST = xStripSlashesGPC($_POST);
  $_GET = xStripSlashesGPC($_GET);
  $_COOKIE = xStripSlashesGPC($_COOKIE);

  
require ("core/smarty/smarty.class.php");
$smarty = new Smarty; //core smarty object
notice_table_create_records();

function qiwipost_tracking_check ()
{//subscrVerifyEmailAddress
$sql = "
		SELECT orderID, tracknumber, shipping_type "
		. "FROM  `" . ORDERS_TABLE . "` "
		. "WHERE  `tracknumber` REGEXP  '^[^0-9]*[0-9]{14,16}$' "
		. "AND `shipping_type` REGEXP  '^.*qiwipost.*$' "
		. "AND StatusID = 4 "
		. "order by orderID asc "
		. "limit 100"
		;
	$q = db_query($sql);
	while ($row = db_fetch_row($q))
	{
		$qp = new QPParcelStatus;
		$QiwiPostStatus = $qp->QPGetParcelStatus(CONF_SHIPPING_QIWIPOSTAPI_LOGIN, CONF_SHIPPING_QIWIPOSTAPI_PASSWORD, 'cp-1251', $row['tracknumber']);
		$row['qiwipost_status'] = $QiwiPostStatus;
		if ($row['qiwipost_status'] == 'Delivered') {
			$sql2 = "
				UPDATE ".ORDERS_TABLE." SET ".
				"StatusID=".(int)5 . " 
				WHERE orderID=" . (int)$row['orderID'] . " 
				";
			$q2 = db_query($sql2);
			$sql3 = "INSERT into " . ORDER_STATUS_CHANGE_LOG_TABLE . " "
			. "SET "
			. "orderID = '" . $row['orderID'] . "' "
			. ", status_name = 'Delivered' "
			. ", status_change_time = NOW() "
			. ", status_comment = 'Autostatus' "
			;
			$q3 = db_query($sql3);
		}
		sleep (1);
	}
}
qiwipost_tracking_check();


?>